--链接wifi
function wifi_connect(ssid,pwd)
    wifi_config(ssid,pwd)
    --debug("Connect To "..ssid..":"..pwd)
    wifi.sta.connect()
end
--配置wifi
function wifi_config(ssid,pwd)
    station_cfg={}
    station_cfg.ssid=ssid
    station_cfg.pwd=pwd
    station_cfg.save=true
    station_cfg.auto=true
    wifi.sta.config(station_cfg)
end
--配置ap模式
function wifi_ap_config(ssid,pwd)
    cfg={
        ssid=ssid,
        pwd=pwd,
        auth=wifi.WPA2_PSK,
        max=1,
        save=false
    }
    wifi.ap.config(cfg)
    wifi.setmode(wifi.SOFTAP)
end
--开始智能配网
function wifi_start_smart()
    wifi.setmode(wifi.STATION)
    wifi.startsmart(0,
        function(ssid, password)
            debug("SSID: "..ssid," PWD: "..password)            
            wifi.sta.autoconnect(1)
        end
    )
end
--清除配置文件
function wifi_clean()
    wifi.sta.clearconfig();
end
function wifi_reset()

end
-- Print AP list that is easier to read
function wifi_get_ap(t) -- (SSID : Authmode, RSSI, BSSID, Channel)
    for ssid,v in pairs(t) do
        local authmode, rssi, bssid, channel = string.match(v, "([^,]+),([^,]+),([^,]+),([^,]+)")
        print(string.format("%32s",ssid).."\t"..bssid.."\t  "..rssi.."\t\t"..authmode.."\t\t\t"..channel)
        if ssid == Status.Wifi.SSID then
            wifi_connect(Status.Wifi.SSID,Status.Wifi.PWD);
            return ;
        end
    end

end
